﻿using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;

namespace PageAdmin.Utils.ORM
{
    internal interface IDbInstance
    {

        DataBaseInfo CurrentDataBaseInfo { get; }

        IDbTransaction DbTransaction { get; }

        DbConnection Conn { get; }

        /// <summary>
        /// 开始事务
        /// </summary>
        IDbTransaction TransactionBegin();

        /// <summary>
        /// 提交事务
        /// </summary>
        void TransactionCommit();

        /// <summary>
        /// 事务回滚
        /// </summary>
        void TransactionRollback();


        /// <summary>
        /// 运行sql语句
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        int Execute(string sql, DynamicParameters dynamicParameters);


        /// <summary>
        /// 返回IDataReader
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        IDataReader GetReader(string sql, DynamicParameters dynamicParameters);

        /// <summary>
        /// 获取DataTable
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="sqlParams"></param>
        /// <returns></returns>
        DataTable GetDataTable(string sql, DynamicParameters dynamicParameters);

        /// <summary>
        /// 获取某个字段值
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        object GetField(string sql, DynamicParameters dynamicParameters);

        /// <summary>
        /// 获取数据表结构
        /// </summary>
        /// <param name="table"></param>
        /// <returns></returns>
        DataTable GetSchemaTable(string table);


        /// <summary>
        /// 获取数据库中所有表名称
        /// </summary>
        /// <returns></returns>
        List<string> GetTables();

        /// <summary>
        /// 判断表是否存在数据库中
        /// </summary>
        /// <returns></returns>
        bool TableIsExists(string table);

        /// <summary>
        /// 判断字段是否存在表中
        /// </summary>
        /// <param name="table"></param>
        /// <param name="columnName"></param>
        /// <returns></returns>
        bool ColumnIsExists(string table, string columnName);


        /// <summary>
        /// 创建表
        /// </summary>
        /// <param name="name"></param>
        /// <param name="dataColumnInfoList"></param>
        void CreateTable(string name, IEnumerable<TableColumnInfo> dataColumnInfoList);

        /// <summary>
        /// 创建字段
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="dataColumnInfo"></param>
        void CreateColumn(string tableName, TableColumnInfo dataColumnInfo);


        /// <summary>
        /// 删除表
        /// </summary>
        /// <param name="name"></param>
        void DropTable(string name);

        /// <summary>
        /// 删除字段
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="name"></param>
        void DropColumn(string tableName, string name);

        /// <summary>
        /// 修改表名
        /// </summary>
        /// <param name="name"></param>
        /// <param name="targetName"></param>
        void RenameTable(string name, string targetName);

        /// <summary>
        /// 修改字段名
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="name"></param>
        /// <param name="targetName"></param>
        void RenameColumn(string tableName, string name, string targetName);


        /// <summary>
        /// 修改字段属性
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="dataColumnInfo"></param>
        void AlterColumn(string tableName, TableColumnInfo dataColumnInfo);



        /// <summary>
        /// 更改字段默认值
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="tableColumnInfo"></param>
        void AlterColumnDefaultValue(string tableName, TableColumnInfo tableColumnInfo);


        /// <summary>
        /// 移出默认值约束
        /// </summary>
        /// <param name="table"></param>
        /// <param name="field"></param>
        void RemoveColumnDefaultValue(string table, string field);

        /// <summary>
        /// 关闭连接，释放所有资源
        /// </summary>
        void Dispose();
    }
}
